package com.lr.students.service.impl;

import com.lr.students.dao.ClassessMapper;
import com.lr.students.dao.StudentsMapper;
import com.lr.students.model.Classess;
import com.lr.students.model.Students;
import com.lr.students.model.StudentsInfo;
import com.lr.students.service.StudentsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;


@Service
public class StudentsServiceImpl implements StudentsService {

    @Autowired
    private StudentsMapper studentsMapper;
    @Autowired
    private ClassessMapper classessMapper;




    @Override
    public PageInfo<StudentsInfo> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        List<Students> students = studentsMapper.selectAll();
        List<StudentsInfo> studentsList = new ArrayList<>();
        for (Students student : students) {
            StudentsInfo studentsInfo = new StudentsInfo();
            studentsInfo.setStudentId(student.getStudentId());
            studentsInfo.setStudentName(student.getStudentName());
            studentsInfo.setStudentGender(student.getStudentGender());
            studentsInfo.setStudentBirthday(student.getStudentBirthday());
            studentsInfo.setCellphone(student.getCellphone());

            studentsInfo.setclassess(classessMapper.selectByPrimaryKey(student.getclassessId()));

            studentsList.add(studentsInfo);
        }
        return new PageInfo<StudentsInfo>(studentsList);
    }



    @Override
    public void delete(String id){
        studentsMapper.deleteByPrimaryKey(id);
    }


    @Override
    public void update(Students students){
        studentsMapper.updateByPrimaryKey(students);
    }


    @Override
    public void add(Students students){
        studentsMapper.insert(students);
    }

    @Override
    public StudentsInfo findById(String id){
        Students student = studentsMapper.selectByPrimaryKey(id);

            StudentsInfo studentsInfo = new StudentsInfo();
            studentsInfo.setStudentId(student.getStudentId());
            studentsInfo.setStudentName(student.getStudentName());
            studentsInfo.setStudentGender(student.getStudentGender());
            studentsInfo.setStudentBirthday(student.getStudentBirthday());
            studentsInfo.setCellphone(student.getCellphone());

            studentsInfo.setclassess(classessMapper.selectByPrimaryKey(student.getclassessId()));



        return studentsInfo;
    }


    @Override
    public List<StudentsInfo> findAll() {
        List<Students> students = studentsMapper.selectAll();
        List<StudentsInfo> studentsList = new ArrayList<>();
        for (Students student : students) {
            StudentsInfo studentsInfo = new StudentsInfo();
            studentsInfo.setStudentId(student.getStudentId());
            studentsInfo.setStudentName(student.getStudentName());
            studentsInfo.setStudentGender(student.getStudentGender());
            studentsInfo.setStudentBirthday(student.getStudentBirthday());
            studentsInfo.setCellphone(student.getCellphone());

            studentsInfo.setclassess(classessMapper.selectByPrimaryKey(student.getclassessId()));

            studentsList.add(studentsInfo);
        }
        return studentsList;
    }


    @Override
    public List<Classess> findclassessAll() {
         return classessMapper.selectAll();
    }
}
